package tgc.edu.exam.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.exam.custom.AjaxResult;
import tgc.edu.exam.custom.CommonController;
import tgc.edu.exam.entity.Permission;
import tgc.edu.exam.entity.SysRole;
import tgc.edu.exam.entity.TreeTableModel;
import tgc.edu.exam.service.PermissionService;
import tgc.edu.exam.service.SysRoleService;
import tgc.edu.exam.web.form.SysRoleForm;

@Controller
@RequestMapping(value="/role")
public class SysRoleController extends CommonController<SysRole, UUID, SysRoleForm>{
	@Autowired
	private SysRoleService service;
	
	@Autowired
	private PermissionService permissionService;
	
	@Override
	public void edit(SysRoleForm form, ModelMap map) throws InstantiationException, IllegalAccessException {
		SysRole model=new SysRole();
		UUID id = form.getId();
		String parentId = form.getParentId();
		if(id!=null) {
			model=service.findById(id);
			parentId=model.getParentId();
		}
		if(StringUtils.hasText(parentId)&&!"0".equals(parentId)) {
			SysRole parent = service.findById(UUID.fromString(parentId));
			model.setParent(parent);
		}
		map.put("model", model);
		if(parentId!=null) {
			
			List<Permission> permissions = permissionService.findAll(Sort.by("idx1"));
			map.put("roles", permissions);
		}
	}


	@Override
	public Object save(SysRoleForm form) {
		try {
			SysRole model=new SysRole();
			UUID id = form.getId();
			if(id!=null) {
				model=service.findById(id);
			}
			String parentId = form.getParentId();
			if(StringUtils.hasText(parentId)&&!"0".equals(parentId)) {
				model.setParent(service.findById(UUID.fromString(parentId)));
			}else {
				model.setParent(null);
			}
			String permissionCode = form.getPermissionCode();
			if(StringUtils.hasText(permissionCode)) {
				Permission permission=permissionService.findByCode(permissionCode);
				model.setName(permission.getName());
				model.setCode(permission.getCode());
			}else {
				model.setName(form.getName());
				model.setCode(form.getCode());
			}
			model.setIdx1(form.getIdx1());
			service.save(model);
			return new AjaxResult("数据保存成功");
		} catch (Exception e) {
			return new AjaxResult(false, "数据保存失败");
		}
		
	}
	
	@RequestMapping(value="/combotree")
	@ResponseBody
	public Object combotree(UUID id) {
		List<SysRole> list=service.findByParentIsNull();
		return buildCombobtree(list,id);
	}


	private Object buildCombobtree(List<SysRole> list, UUID id) {
		List<HashMap<String, Object>> result=new ArrayList<>();
		for (SysRole role : list) {
			if(role.getId()!=id) {
				HashMap<String, Object> node=new HashMap<>();
				node.put("id", role.getId());
				node.put("text",role.getName());
				node.put("pid", role.getParentId());
				node.put("nodes", buildCombobtree(role.getChildren(), id));
				result.add(node);
			}
		}
		return result;
	}
	
	@RequestMapping(value="/treeList")
	@ResponseBody
	public Object treeList() {
		Sort sort=Sort.by("idx1");
		List<SysRole> list = service.findAll(sort);
		return new TreeTableModel(list);
	}
	
}
